package pl.arekczogala.plati;

import java.io.BufferedReader;
import java.io.IOException;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import java.net.UnknownHostException;

public class NetworkUDP {

Integer port = 65000;
	
	String str_con;
	
	
	InetAddress remoteIP;
	

	DatagramPacket packet_udp;
	DatagramSocket socket_udp;
	
	byte[] data = new byte[1500];
	
	PrintWriter output;
	OutputStream out;
	
	BufferedReader input;
	
	public NetworkUDP(){

	    
	}
	
	public String connect(String addressIP){
		
		
		try{
			InetAddress remoteIP = InetAddress.getByName(addressIP);
	    
			packet_udp = new DatagramPacket(data, data.length,remoteIP,port);
			socket_udp = new DatagramSocket(port);
			 
			

		 } catch (UnknownHostException e) {
	            // TODO Auto-generated catch block
	            return (e.getMessage());
		   } catch (IOException e) {
	            // TODO Auto-generated catch block
	            return (e.getMessage());
	}
	return "Connected" ;
	}
	
	public String disconnect(){
	
		socket_udp.close();
		
		return "Disconnected";
	}
	public void send(String data){
					
	try{
			packet_udp.setData(data.getBytes());
			socket_udp.send(packet_udp);
			
			
		 } catch (UnknownHostException e) {
	            // TODO Auto-generated catch block
	            e.getMessage();
		   } catch (IOException e) {
	            // TODO Auto-generated catch block
	            e.getMessage();
	}       

	}
	
	
}
